/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * ExternalizeActor.java
 * Copyright (C) 2014-2018 University of Waikato, Hamilton, NZ
 */
package adams.gui.flow.tree.menu;

import adams.flow.core.Actor;
import adams.gui.core.BaseMenu;

import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;

/**
 * For turning an actor into an external one.
 * 
 * @author fracpete
 */
public class ExternalizeActor
  extends AbstractTreePopupMenuItemAction {

  /** for serialization. */
  private static final long serialVersionUID = 3991575839421394939L;
  
  /**
   * Returns the caption of this action.
   * 
   * @return		the caption, null if not applicable
   */
  @Override
  protected String getTitle() {
    return "Externalize";
  }

  /**
   * Creates a new menuitem using itself.
   */
  @Override
  public JMenuItem getMenuItem() {
    BaseMenu 		result;
    JMenuItem 		menuitem;
    Actor[] 		suggestions;

    if (m_State.selPaths == null)
      return null;

    result = new BaseMenu(getName());
    result.setEnabled(isEnabled());
    result.setIcon(getIcon());
    suggestions = m_State.tree.getOperations().suggestExternalActors(m_State.selPaths);
    if (suggestions == null) {
      menuitem = new JMenuItem("Automatic");
      menuitem.addActionListener((ActionEvent e) ->
	m_State.tree.getOperations().externalizeActor(m_State.selPaths, null)
      );
      result.add(menuitem);
    }
    else {
      for (Actor s : suggestions) {
	final Actor suggestion = s;
	menuitem = new JMenuItem(suggestion.getName());
	menuitem.addActionListener((ActionEvent e) ->
	  m_State.tree.getOperations().externalizeActor(m_State.selPaths, suggestion)
	);
	result.add(menuitem);
      }
    }

    return result;
  }

  /**
   * Updates the action using the current state information.
   */
  @Override
  protected void doUpdate() {
    setEnabled(m_State.editable && (m_State.numSel >= 1) && (m_State.tree.getOwner() != null));
  }

  /**
   * The action to execute.
   *
   * @param e		the event
   */
  @Override
  protected void doActionPerformed(ActionEvent e) {
    // obsolete
  }
}
